<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      {{msg}}
      <button @click="aikun">侦听</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script>
      //点击按钮的时候才开始侦听
      const vm = new Vue({
        el: "#app",
        data: {
          msg: "多喝热水",
        },
        methods: {
          aikun() {
             this.$watch("msg", function(newval,oldval){
                  console.log(newval);
                  console.log(oldval);
             })
          },
        },
        // watch: {
        //   //让侦听函数立即调用
        //   msg: {
        //     handler(newval, oldval) {
        //       console.log(newval);
        //       console.log(oldval);
        //     },
        //     immediate: true,
        //   },
        // },
      });
    </script>
  </body>
</html>
